<?php
 /*
  *总后台商品模型类
  */
namespace Admin\Model; 
use Think\Model;
class ItemModel extends Model{
	
	public function index($arg=array())
	{	
		$yid=$_SESSION['yid'];
		$page=intval($arg['page']);
		$page=!empty($page)?$page:1;          
		 $condition=' 1=1 ';        
        //where 语句 
 
        /* if($arg['platid']==1)
        {
              $condition.= ' AND   yid=0  AND  platid !=0';  //平台产品
        }else
        {
             
               $condition.= ' AND   yid='.$yid;////运营商产品
        }*/

		if(!empty($arg['typeid'])){
			$condition='typeid='.$arg['typeid'];
		}/*else{
			$condition.='1=1';
		}*/
		if(!empty($arg['brandid'])){
			$condition.=' AND brandid='.$arg['brandid'];
		}
		if(!empty($arg['keywords'])){
			$condition.=" AND itemname like '%".$arg['keywords']."%' or bn like '%".$arg['keywords']."%'";
		}
         if(!empty($arg['platid']))
        {
            $condition.= ' AND   yid=0  AND  platid !=0';  //平台产品
              $condition.='  AND  examine=1 AND status=1';////平台已经审核过的
        }else
        {
               $condition.= ' AND   yid='.$yid;////运营商产品
        }
		

		$count=M()->query("select count(*) as count from ".C(DB_PREFIX).'item where '.$condition);

 		
 		$pagearr= getPageArr($page,C('PAGESIZE'),$count[0]['count']);
 		$result=M('item')
 		->where($condition)
 		->limit($pagearr['start'],$pagearr['pagesize'])
 		->order('itemid desc')
 		->getField('itemid,itemname,bn,price,inventory,FROM_UNIXTIME(createtime, "%Y-%m-%d %H:%i:%S") createtime,supperprice,yid,status,examine');
 	

 		foreach ($result as $key => &$value) 
 		{
 			if ($value['examine']==0) {
 				$value['isshen']='未审核';
 			}else{
 				$value['isshen']='已审核';
 			}
 			if($value['yid'] ==$_SESSION['yid'])
 			{
 				if($value['status']==1 && $value['examine']==1){
 					$value['itemstatus']='已上架';
 					$value['items']=1;
 				}elseif ($value['status']==2 && $value['examine']==1) {
 					$value['itemstatus']='已下架';
 					$value['items']=2;
 				}else{
 					$value['itemstatus']='';
 				}
 			}

 			
 		}
 		///rvar_dump($result);

 		
 		$temp['pagearr']=$pagearr;
 		$temp['item']=!empty($result)?$result:'';
 		//设置page cookie
 		cookie('page',$page,120);
 		return $temp;

	}

}